/*     */ package com.zimbra.qa.unittest.server;
/*     */ 
/*     */ import com.zimbra.client.ZDataSource;
/*     */ import com.zimbra.client.ZFilterRules;
/*     */ import com.zimbra.client.ZMailbox;
/*     */ import com.zimbra.client.ZPop3DataSource;
/*     */ import com.zimbra.common.account.Key.DataSourceBy;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.DataSource;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.db.DbPop3Message;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.qa.unittest.TestUtil;
/*     */ import com.zimbra.soap.admin.type.DataSourceType;
/*     */ import java.util.HashMap;
/*     */ import java.util.HashSet;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import junit.framework.TestCase;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestPop3ImportServer
/*     */   extends TestCase
/*     */ {
/*     */   private static final String USER_NAME = "user1";
/*  47 */   private static final String NAME_PREFIX = TestPop3ImportServer.class.getSimpleName();
/*  48 */   private static final String DATA_SOURCE_NAME = NAME_PREFIX;
/*  49 */   private static final String TEMP_USER_NAME = NAME_PREFIX + "Temp";
/*     */   private ZFilterRules mOriginalRules;
/*     */   private boolean mIsServerSideTest;
/*     */   
/*     */   public void setUp()
/*     */     throws Exception
/*     */   {
/*  56 */     this.mIsServerSideTest = false;
/*  57 */     cleanUp();
/*  58 */     createDataSource();
/*  59 */     this.mOriginalRules = TestUtil.getZMailbox("user1").getIncomingFilterRules();
/*     */   }
/*     */   
/*     */ 
/*     */   public void testUidPersistence()
/*     */     throws Exception
/*     */   {
/*  66 */     this.mIsServerSideTest = true;
/*  67 */     DataSource ds = getDataSource();
/*  68 */     Mailbox mbox = TestUtil.getMailbox("user1");
/*     */     
/*     */ 
/*     */ 
/*  72 */     String uid1 = "myUID";
/*  73 */     String uid2 = "myUid";
/*  74 */     Set<String> uids = new HashSet();
/*  75 */     uids.add(uid1);
/*  76 */     uids.add(uid2);
/*     */     
/*     */ 
/*  79 */     Set<String> matchingUids = DbPop3Message.getMatchingUids(mbox, ds, uids);
/*  80 */     assertEquals("Test 1: set size", 0, matchingUids.size());
/*     */     
/*     */ 
/*  83 */     DbPop3Message.storeUid(mbox, ds.getId(), uid1, 2);
/*  84 */     matchingUids = DbPop3Message.getMatchingUids(mbox, ds, uids);
/*  85 */     assertEquals("Test 2: set size", 1, matchingUids.size());
/*  86 */     assertTrue("Test 2: did not find uid1", matchingUids.contains(uid1));
/*  87 */     assertFalse("Test 2: found uid2", matchingUids.contains(uid2));
/*     */     
/*     */ 
/*  90 */     DbPop3Message.storeUid(mbox, ds.getId(), uid2, 3);
/*  91 */     matchingUids = DbPop3Message.getMatchingUids(mbox, ds, uids);
/*  92 */     assertEquals("Test 3: set size", 2, matchingUids.size());
/*  93 */     assertTrue("Test 3: did not find uid1", matchingUids.contains(uid1));
/*  94 */     assertTrue("Test 3: did not find uid2", matchingUids.contains(uid2));
/*     */     
/*     */ 
/*  97 */     DbPop3Message.deleteUids(mbox, ds.getId());
/*  98 */     matchingUids = DbPop3Message.getMatchingUids(mbox, ds, uids);
/*  99 */     assertEquals("Test 3: set size", 0, matchingUids.size());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void testModifyDataSource()
/*     */     throws Exception
/*     */   {
/* 107 */     this.mIsServerSideTest = true;
/*     */     
/*     */ 
/* 110 */     ZPop3DataSource zds = getZDataSource();
/* 111 */     zds.setHost(zds.getHost() + "2");
/* 112 */     modifyAndCheck(zds, false);
/*     */     
/*     */ 
/* 115 */     zds = getZDataSource();
/* 116 */     zds.setUsername(zds.getUsername() + "2");
/* 117 */     modifyAndCheck(zds, false);
/*     */     
/*     */ 
/* 120 */     zds = getZDataSource();
/* 121 */     ZimbraLog.test.info("Old leaveOnServer value: " + zds.leaveOnServer());
/* 122 */     zds.setLeaveOnServer(!zds.leaveOnServer());
/* 123 */     modifyAndCheck(zds, true);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void testDeleteMailbox()
/*     */     throws Exception
/*     */   {
/* 131 */     this.mIsServerSideTest = true;
/*     */     
/*     */ 
/* 134 */     Provisioning prov = Provisioning.getInstance();
/* 135 */     Account account = prov.createAccount(TestUtil.getAddress(TEMP_USER_NAME), "test123", null);
/* 136 */     Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
/*     */     
/*     */ 
/* 139 */     DbPop3Message.storeUid(mbox, "TestPop3Import", "uid1", 2);
/* 140 */     mbox.deleteMailbox();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private void modifyAndCheck(ZPop3DataSource zds, boolean shouldDeleteData)
/*     */     throws Exception
/*     */   {
/* 148 */     Mailbox mbox = TestUtil.getMailbox("user1");
/* 149 */     DataSource ds = getDataSource();
/* 150 */     ZMailbox zmbox = TestUtil.getZMailbox("user1");
/*     */     
/*     */ 
/* 153 */     DbPop3Message.deleteUids(mbox, ds.getId());
/* 154 */     DbPop3Message.storeUid(mbox, ds.getId(), "1", 2);
/*     */     
/*     */ 
/*     */ 
/* 158 */     zmbox.modifyDataSource(zds);
/*     */     
/* 160 */     Set<String> uids = new HashSet();
/* 161 */     uids.add("1");
/* 162 */     Set<String> matchingUids = DbPop3Message.getMatchingUids(mbox, ds, uids);
/* 163 */     int expected = shouldDeleteData ? 0 : 1;
/* 164 */     assertEquals("matching UID's: " + StringUtil.join(",", matchingUids), expected, matchingUids.size());
/*     */   }
/*     */   
/*     */   private ZPop3DataSource getZDataSource() throws Exception {
/* 168 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 169 */     List<ZDataSource> dataSources = mbox.getAllDataSources();
/* 170 */     for (ZDataSource ds : dataSources) {
/* 171 */       if (ds.getName().equals(DATA_SOURCE_NAME)) {
/* 172 */         return (ZPop3DataSource)ds;
/*     */       }
/*     */     }
/* 175 */     fail("Could not find data source " + DATA_SOURCE_NAME);
/* 176 */     return null;
/*     */   }
/*     */   
/*     */   public void tearDown() throws Exception
/*     */   {
/* 181 */     cleanUp();
/* 182 */     TestUtil.getZMailbox("user1").saveIncomingFilterRules(this.mOriginalRules);
/*     */   }
/*     */   
/*     */   private void createDataSource() throws Exception {
/* 186 */     Provisioning prov = Provisioning.getInstance();
/* 187 */     Account account = TestUtil.getAccount("user1");
/* 188 */     int port = Integer.parseInt(TestUtil.getServerAttr("zimbraPop3BindPort"));
/* 189 */     Map<String, Object> attrs = new HashMap();
/* 190 */     attrs.put("zimbraDataSourceEnabled", "FALSE");
/* 191 */     attrs.put("zimbraDataSourceHost", "localhost");
/* 192 */     attrs.put("zimbraDataSourcePort", Integer.toString(port));
/* 193 */     attrs.put("zimbraDataSourceUsername", "user1");
/* 194 */     attrs.put("zimbraDataSourcePassword", "test123");
/* 195 */     attrs.put("zimbraDataSourceFolderId", Integer.toString(2));
/* 196 */     attrs.put("zimbraDataSourceConnectionType", "cleartext");
/* 197 */     attrs.put("zimbraDataSourceLeaveOnServer", "FALSE");
/* 198 */     prov.createDataSource(account, DataSourceType.pop3, DATA_SOURCE_NAME, attrs);
/*     */   }
/*     */   
/*     */   private DataSource getDataSource() throws Exception {
/* 202 */     Provisioning prov = Provisioning.getInstance();
/* 203 */     Account account = TestUtil.getAccount("user1");
/* 204 */     return prov.get(account, Key.DataSourceBy.name, DATA_SOURCE_NAME);
/*     */   }
/*     */   
/*     */   private void cleanUp() throws Exception
/*     */   {
/* 209 */     Provisioning prov = Provisioning.getInstance();
/* 210 */     DataSource ds = getDataSource();
/* 211 */     if (ds != null) {
/* 212 */       Account account = TestUtil.getAccount("user1");
/* 213 */       if (this.mIsServerSideTest) {
/* 214 */         Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
/* 215 */         DbPop3Message.deleteUids(mbox, ds.getId());
/*     */       }
/* 217 */       prov.deleteDataSource(account, ds.getId());
/*     */     }
/* 219 */     TestUtil.deleteTestData("user1", NAME_PREFIX);
/* 220 */     TestUtil.deleteAccount(TEMP_USER_NAME);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/server/TestPop3ImportServer.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */